﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using woaC2.SocketLib;

namespace woaPoker.LogicLib.LogicCtrl
{
    public class MathRegistry
    {
        private MathRegistry()
        {
        }

        internal static MathRegistry instance
        {
            get
            {
                return Nested.instance;
            }
        }
        private class Nested
        {
            static Nested()
            {
            }
            internal static readonly MathRegistry instance = new MathRegistry();
        }


        public readonly ConcurrentDictionary<Guid, Match> listMath = new ConcurrentDictionary<Guid, Match>();

        //public List<Match> listMath1 = new List<Match>();
        
        internal int Count()
        {
            return this.listMath.Count();
           
        }
        
        internal void AddMatch(Match match)
        {
            this.listMath.TryAdd(match.sessionIdMatch, match);
        }

        internal Match GetMatchById(Guid sessionId)
        {
            if (this.listMath.ContainsKey(sessionId))
            {
                return this.listMath[sessionId];
            }
            return null;
        }

        internal bool Contains(Guid sessionId)
        {
            return this.listMath.ContainsKey(sessionId);
        }

        internal void SetStatus(Guid sessionIdMatch,bool status)
        {
            this.listMath[sessionIdMatch].status = status;
        }

        internal List<Match> GetAllMatchFree()
        {
            return listMath.Where(k=>k.Value.status==false).Select(k=>k.Value).ToList();
        }

        public bool Remove(Guid sessionId)
        {
            try
            {
                if (listMath.ContainsKey(sessionId))
                {
                    Match val;
                    //listMath[sessionId].TcpClient.Client.Close();
                    //listMath[sessionId].TcpClient.Client.Dispose();
                    //listMath[sessionId].TcpClient.Close();
                    this.listMath.TryRemove(sessionId, out val);
                }
                return true;
            }
            catch (ArgumentNullException ex)
            {
                SocketLog.Write("[MatchRegistry.Remove]--[ERROR]--" + ex);
                return false;
            }
        }
        public int AddPlayer(Player player, Guid sessionMatchId)
        {
            if (this.listMath[sessionMatchId].listPlayer1.Count > this.listMath[sessionMatchId].listPlayer2.Count&& this.listMath[sessionMatchId].listPlayer2.Count<3)
            {
                this.listMath[sessionMatchId].listPlayer2.Add(player);
                return 2;
            }
            else if(this.listMath[sessionMatchId].listPlayer1.Count<3)
            {
                this.listMath[sessionMatchId].listPlayer1.Add(player);
                return 1;
            }
            return 0;

        }
        public Match FindMatchByPlayerId(Guid sessionId)
        {
            foreach (Match match in listMath.Select(s=>s.Value))
            {
                if (match.listPlayer1.Where(k => k.client.SessionID == sessionId).FirstOrDefault()!=null)
                {
                    return match;
                }
                else if(match.listPlayer2.Where(k => k.client.SessionID == sessionId).FirstOrDefault()!=null)
                return match;
                
            }
            return null;
        }
    }
}
